package graphic.dialogs;

import graphic.FilesTabbedPanel;
import graphic.MainFrame;
import graphic.mycomponents.TextChecked;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.IOException;
import java.util.HashMap;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import utility.GraphicUtilsFactory;
import core.Function;
import core.Server;
import dbms.DBGeneralFunctions;
import dbms.DBParameters;

public class ProgramSettingsDialog extends AbstractDialog
{
	TextChecked programName;
	TextChecked newProgramName;	
	JCheckBox newFiles;
	
	HashMap<String, String> programSettings;
	
	public ProgramSettingsDialog()
	{	
		super("Program Settings", "Save", 600, 350);
	}
	
	public void createPanel()
	{
		this.programSettings = DBGeneralFunctions.getProgramConfigurationMap(FilesTabbedPanel.getInstance().getSelectedTab());

		super.setPanel(new JPanel(new GridBagLayout()));
		
		JPanel panel = super.getPanel(); 
		
		this.programName = new TextChecked(this.programSettings.get(DBParameters.NAME), TextChecked.TEXT, false, false, null);
		
		this.newProgramName = new TextChecked("", TextChecked.TEXT, true, true, Function.getProgramNamesList());
		this.newFiles = new JCheckBox("Allow users to create new files");

		if(this.programSettings.get(DBParameters.NEW_FILE).equals(DBParameters.YES))
			this.newFiles.setSelected(true);
		else
			this.newFiles.setSelected(false);
		
		JPanel checkBoxPanel = new JPanel();
		checkBoxPanel.add(this.newFiles);
		
		GraphicUtilsFactory.setTitleAndBorderToComponent(this.programName, "Program Name");		
		GraphicUtilsFactory.setTitleAndBorderToComponent(this.newProgramName, "Change Program Name");		
		GraphicUtilsFactory.setTitleAndBorderToComponent(checkBoxPanel, "Settings");		
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.CENTER;
		c.insets = new Insets(5, 10, 5, 10);
		
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		panel.add(new JLabel("Program Settings"), c);
		
		c.gridx = 0;
		c.gridy = 1;
		panel.add(this.programName, c);
		
		c.gridx = 0;
		c.gridy = 2;
		panel.add(this.newProgramName, c);		

		c.gridx = 0;
		c.gridy = 3;
		panel.add(checkBoxPanel, c);
		
		c.gridx = 0;
		c.gridy = 4;
		c.gridwidth = 1;
		panel.add(super.getOkButton(), c);

		c.gridx = 1;
		c.gridy = 4;
		panel.add(super.getExitButton(), c);
	}

	@Override
	public boolean checkOkFunction()
	{ 
		if(!this.newProgramName.isChecked())
		{		
			MainFrame.getInstance().showMessage("New program name incorrect!", JOptionPane.WARNING_MESSAGE);
			return false;
		}
		
		int reply = JOptionPane.showConfirmDialog(null, "Server will be stopped to change settings!\nEvery process will be interrupted.", "Stopping the Server", JOptionPane.YES_NO_OPTION);
		if (reply == JOptionPane.YES_OPTION)
			return true;
		else
			return false;
	}

	@Override
	public void okFunction()
	{
		try { Server.getInstance().stopServer(); }
		catch (IOException e) {}
		finally {
		
			if(!this.newProgramName.getText().isEmpty())
				if(Function.changeProgramName(this.programName.getText(), this.newProgramName.getText()))
				{
					String new_files;
					if(this.newFiles.isSelected())
						new_files = DBParameters.YES;
					else
						new_files = DBParameters.NO;
					
					if(Function.setProgramParameter(new_files, DBParameters.NEW_FILE, new_files))
					{
						super.dispose();
						MainFrame.getInstance().showMessage("Program Settings Saved!", JOptionPane.INFORMATION_MESSAGE);
						FilesTabbedPanel.getInstance().getTabs().get(FilesTabbedPanel.getInstance().getSelectedTab()).createTable();
					} else {
						super.dispose();
						MainFrame.getInstance().showMessage("Program Settings NOT Saved!", JOptionPane.ERROR_MESSAGE);
					}
				
				} else {
					super.dispose();
					MainFrame.getInstance().showMessage("Error while setting new program name!", JOptionPane.ERROR_MESSAGE);
			}
		}
	}	
		
}